package com.zjff.show.zjffshow.controller;

import com.zjff.show.zjffshow.entity.ExpressDao;
import com.zjff.show.zjffshow.entity.ResultDao;
import com.zjff.show.zjffshow.service.ExpressService;
import com.zjff.show.zjffshow.tool.JSONTool;
import com.zjff.show.zjffshow.tool.RedisTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Express/")
public class ExpressController {

    @Autowired
    ExpressService expressService;
    ResultDao resultDao = null;
    Jedis jedis = new RedisTool().getJedis();

    //获取快递状态
    @GetMapping("/getExpress")
    private String getExpress(@RequestParam("consignee") String consignee){
        resultDao = new ResultDao();
        resultDao.setData(expressService.getExpress(consignee).getStatus());
        return JSONTool.getJsonTool().toJson(resultDao);
    }
    //获取用户列表
    @RequestMapping(value = "/getExpressList",method = RequestMethod.GET)
    private String getExpressList(HttpServletRequest request){
        resultDao = new ResultDao();
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String id = request.getParameter("id");
        String consignor = request.getParameter("consignor");

        int intid = 0;
        boolean flag = false;
        if (id!=null && !id.equals("") && !id.equals("0")){
            intid = Integer.parseInt(id);
            flag = true;
        }
        if (!consignor.equals("")){
            flag = true;
        }
        int Start=(page-1)*limit;//设置起始位置
        List<ExpressDao> list = expressService.getExpressList(intid,Start,limit,consignor);
        resultDao.setCode("0");
        resultDao.setMsg("获取成功！");
        if (flag){
            resultDao.setCount(String.valueOf(list.size()));
        }else {
            resultDao.setCount(String.valueOf(expressService.getExpressCount()));
        }
        resultDao.setData(list);
        return JSONTool.getJsonTool().toJson(resultDao);
    }

    //获取所有用户数量
    @RequestMapping(value = "/getExpressCount",method = RequestMethod.GET)
    private String getExpressCount(HttpServletRequest request){
        resultDao = new ResultDao();
        int count = expressService.getExpressCount();
        resultDao.setCount(String.valueOf(count));
        return JSONTool.getJsonTool().toJson(resultDao);
    }
    @GetMapping("/upExpress")
    private String upExpress(@RequestParam("id") int id,
                                @RequestParam("Status") String Status
    ){
        resultDao = new ResultDao();
        ExpressDao expressDao = new ExpressDao();
        expressDao.setId(id);
        expressDao.setStatus(Status);
        int result = expressService.upExpress(expressDao);
        if (result>0){
            resultDao.setCode("0");
            resultDao.setMsg("更新成功");
        }else{
            resultDao.setCode("1");
            resultDao.setMsg("更新失败");
        }
        return JSONTool.getJsonTool().toJson(resultDao);
    }
    //获取用户列表
    @RequestMapping(value = "/getAddExpressList",method = RequestMethod.GET)
    private String getAddExpressList(HttpServletRequest request){
        resultDao = new ResultDao();
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String id = request.getParameter("id");
        String consignor = request.getParameter("consignor");

        int intid = 0;
        boolean flag = false;
        if (id!=null && !id.equals("") && !id.equals("0")){
            intid = Integer.parseInt(id);
            flag = true;
        }
        if (!consignor.equals("")){
            flag = true;
        }
        int Start=(page-1)*limit;//设置起始位置
        List<ExpressDao> list = expressService.getAddExpressList(intid,Start,limit,consignor);
        resultDao.setCode("0");
        resultDao.setMsg("获取成功！");
        if (flag){
            resultDao.setCount(String.valueOf(list.size()));
        }else {
            resultDao.setCount(String.valueOf(expressService.getExpressCount()));
        }
        resultDao.setData(list);
        return JSONTool.getJsonTool().toJson(resultDao);
    }

    //获取所有用户数量
    @RequestMapping(value = "/getAddExpressCount",method = RequestMethod.GET)
    private String getAddExpressCount(HttpServletRequest request){
        resultDao = new ResultDao();
        int count = expressService.getExpressCount();
        resultDao.setCount(String.valueOf(count));
        return JSONTool.getJsonTool().toJson(resultDao);
    }
    @GetMapping("/addExpress")
    private String addExpress(@RequestParam("consigness") String consigness,
                             @RequestParam("consignor") String consignor,
                             @RequestParam("DeliveryPlace") String DeliveryPlace,
                             @RequestParam("ReceiptPlace") String ReceiptPlace)
                             {
        resultDao = new ResultDao();
        ExpressDao expressDao = new ExpressDao();
        expressDao.setConsigness(consigness);
        expressDao.setConsignor(consignor);
        expressDao.setDeliveryPlace(DeliveryPlace);
        expressDao.setReceiptPlace(ReceiptPlace);
        int result = expressService.addExpress(expressDao);
        if (result>0){
            resultDao.setCode("0");
            resultDao.setMsg("添加成功");
        }else{
            resultDao.setCode("1");
            resultDao.setMsg("添加失败");
        }
        return JSONTool.getJsonTool().toJson(resultDao);
    }




}
